﻿package app.view.components.sections.expo.max 
{
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.guiDima.switchContent.SwitchContentHistoryDBase;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	
	/**
	 * 1.2 ИСТОРИЯ УЧАСТИЯ
	 * firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class ExpoHistoryMBase extends LabeledSectionBase
	{
		// app.view.components.sections.expo.max.ExpoHistoryMBase
		
		public var switchBox:SwitchContentHistoryDBase;
		
		public function ExpoHistoryMBase() 
		{
			super();
		}
		
		private function setEvents():void
		{
			addEventListener(UIEvent.CLICK_ITEM_EVENT, onClickGridItem, true);
			
		}
		
		private function removeEvents():void
		{
			removeEventListener(UIEvent.CLICK_ITEM_EVENT, onClickGridItem, true);
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			switchBox.destroy();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			//trace('data!!!',_data.id);
			//trace('data!!!',_data.currentId);
			//trace('data!!!',_data.data);
			
			
			setParamHead(HeadMarkerBase.BGD_RED);
			
			var arr:Array = new Array();
			
			/*for (var i:int = 0; i < 20; i++) 
			{
				arr.push('');
			}
			switchBox.data = [arr,arr];*/
			
			//----------------
			var lineArr:Array = new Array();
			var ind:int = 0;
			//
			for each(var object:* in _data.data.history)
			{
				//trace('name = ', object);
				var obj:Object = new Object();
				//trace('name',object.name);
				//trace('year',object.text[1]);
				//trace('city',object.text[2]);
				//trace('country',object.text[3]);
				//trace('text',object.text[4]);
				//trace('image',object.image[1].image_path);
				
				obj.name = object.name;
				obj.year = object.text[1];
				obj.city = object.text[2];
				obj.country = object.text[3];
				obj.text = object.text[4];
				object.image[1].image_path != '' ? obj.image = RemoteProxy.DOMEN + object.image[1].image_path : obj.image = '';
				
				lineArr.push( { id: ind, data:obj } );
				
				//trace('line',lineArr[ind].id);
				ind++;
				//trace('-----------');
			}
			
			
			// разбиваем в массив с массивами по десять штук
			var arrTo20:Array = new Array();
			
			//trace('-------------------------',lineArr.length);
			var n:Number;
			var max_const:Number = 20;
			var max:Number = 20;
			var len:Number = lineArr.length;
			
			n = Math.ceil(len / max);
			
			//trace('********',n);
			
			if (n>1) 
			{
				// разбиваем в массивы по десять
				for (var i:int = 0; i < n; i++) 
				{
					var arr20:Array = new Array();
					
					for (var j:int = 0; j < max; j++) 
					{
						//arr10[j] = lineArr[i * (max) + j];
						//trace('ind',i * max_const + j);
						arr20.push(lineArr[i * max_const + j]);
					}
					//trace('11',arr10[j]);
					//
					len = len - max;
					if (len<max) 
					{
						max = len;
					}else
					{
						
					}
					//trace('---------------------');
					
					arrTo20.push(arr20);
				}
				
				switchBox.data = arrTo20;
			}else
			{
				
				switchBox.data = [lineArr];
			}
			
			setEvents();
		};
		
		
		/**
		 * 
		 * @param	e
		 */
		private function onClickGridItem(e:UIEvent):void 
		{
			removeEvents();
			
			var objInfo:Object = new Object();
			
			//trace('777', _data.params.selected.fullDateString);
			
			objInfo.data = _data;
			objInfo.selectedId = e.data.id;
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_COUNTRY_HISTORY;
			obj.currentId = id;
			obj.params = objInfo;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
			
		}
		
	}

}